package br.pucrs.lognplay.faces.bean;

import java.util.List;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

import br.pucrs.lognplay.entity.Client;
import br.pucrs.lognplay.entity.User;
import br.pucrs.lognplay.entity.User.Role;
import br.pucrs.lognplay.service.UserManagementService;

@ManagedBean
@RequestScoped
public class UserListBean extends Bean {

	private List<User> users;

	@EJB
	private UserManagementService userManagementService;

	public List<User> getUsers() {
		try {
			if (users == null) {
				Role loggedUserRole = getLoggedUser().getRole();

				if (loggedUserRole.equals(Role.MASTER_USER)) {
					users = userManagementService.listAllUsers();
				} else if (loggedUserRole.equals(Role.CLIENT_MASTER_USER)) {
					Client loggedUserClient = getLoggedUser().getClient();
					users = userManagementService.listUsersByClientId(loggedUserClient.getId());
				}
			}

			return users;
		} catch (Exception e) {
			throw new BeanException("Unexpected error", e);
		}
	}
}
